package edu.zsc.control;

import edu.zsc.ElevatorConfig;

import java.util.List;

public class Elevator {

    public static final int STAN_COUNT = ElevatorConfig.STAN_COUNT;

    public static final double STOP_TIME = ElevatorConfig.STOP_TIME;

    public static final double EACH_FLOOR_HEIGHT = ElevatorConfig.EACH_FLOOR_HEIGHT;

    public static final int FLOOR_COUNT = ElevatorConfig.FLOOR_COUNT;


    // 当前电梯所在的高度
    private double height;

    // 当前电梯的运行方向
    private ElevatorDirection direction;

    // 电梯的任务列表
    private List<ElevatorTask> tasks;

    // 电梯内部的人数
    private int peopleCount;

    // 电梯还需要停止的时间
    private double waitTime;

    public void acceptTask(ElevatorTask task) {
        this.tasks.add(task);
    }

    // 根据高度得到电梯当前所在楼层
    public int computeFloor() {
        int floor = 0;
        for (int i = 0; i < FLOOR_COUNT; i++) {
            if (Math.abs(this.height - i * EACH_FLOOR_HEIGHT) < 1e6) {
                floor = i;
            }
        }
        return floor;
    }

    public void openDoor() {
        this.direction = ElevatorDirection.OPEN;
        this.waitTime = STOP_TIME;
    }

    public void goUp() {
        assert waitTime != 0;
        //==0
        this.direction = ElevatorDirection.UP;
    }

    public void goDown() {
        assert waitTime != 0;
        //==0
        this.direction = ElevatorDirection.DOWN;
    }

    public void stop() {
        assert waitTime != 0;
        assert !tasks.isEmpty();
        //isEmpty();
        this.direction = ElevatorDirection.STOP;
    }

    public void subWaitTime(double timespan) {
        assert waitTime != 0;
        this.waitTime -= timespan;
    }
}
